# -*- coding: utf-8 -*-
from numpy import *;
def loadDataSet():
    dataMat = []; labelMat = []
    fr = open('txtSet.txt')
    for line in fr.readlines():
        lineArr = line.strip().split()
        dataMat.append([100, float(lineArr[2]), float (lineArr[1])])
        labelMat.append(int (lineArr[3]))
    return dataMat,labelMat

def sigmoid(inX):
    m,n = shape(inX)
    arr = []
    for row in range(m):
        row_arr = []
        for col in range(n):
            if inX[row,col] > 0 :
                a = 1.0/(1+exp(-inX[row,col]))
            else:
                a = exp(inX[row,col])/(1+exp(inX[row,col]))
            row_arr.append(a)
        arr.append(row_arr)
    return mat(arr)
def gradAscent(dataMatIn, classLabels):
    dataMatrix = mat(dataMatIn)
    labelMat = mat(classLabels).transpose()
    m,n = shape(dataMatrix)
    alpha = 0.001
    maxCycles = 2000
    weights = ones((n,1))
    for k in range(maxCycles):
        h = sigmoid(dataMatrix*weights)
        error = (labelMat - h)
        weights = weights + alpha * dataMatrix.transpose()* error
    return weights

def plotBestFit(wei):
    import matplotlib.pyplot as plt
    weights = wei.getA()
    dataMat,labelMat = loadDataSet()
    dataArr = array(dataMat)
    n = shape(dataArr)[0]
    xcord1 = []; ycord1 = []
    xcord2 = []; ycord2 = []
    for i in range(n):
        if int(labelMat[i]) == 1:
            xcord1.append(dataArr[i,1]);ycord1.append(dataArr[i,2])
        else:
            xcord2.append(dataArr[i,1]);ycord2.append(dataArr[i,2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='green')
    x = arange(34,45,0.1)
    y = (-weights[0]*100-weights[1] * x)/weights[2]
    ax.plot(x,y)
    plt.xlabel('X1');plt.ylabel('X2')
    plt.show()

dataMat,labelMat = loadDataSet()
weights = gradAscent(dataMat,labelMat)
print weights
plotBestFit(weights)
